var targetDom = null;
var currentList = {};// tchId:{iProdId:'',code:''}
$(function () {

    $('#ff').on('click',('.textbox-button'),function () {  //textBox按钮点击事件
        targetDom = $(this).parent().parent().find('.teacher');
        window.top.openScan();
    });


    
    if ($('#addTchProd').length > 0) {
        $('#addTchProd').click(function () {
            var dom = '<div class="modelTchTr" style="margin-bottom: 10px;height: 60px;">\n' +
                '        <div class="leftDiv">\n' +
                '            <select  class="easyui-combobox tchProd"   label="教师机类型:" data-options="editable:false,labelPosition:\'top\'" style="width:100%">\n' +
                '                <option value="" style="display: none" selected>请选择</option>\n' ;
            $.each(prods,function (id,text) {
                dom += '<option value="'+id+'">'+text+'</option>\n' ;
            });
            dom+='            </select>\n' +
                '        </div>\n' +
                '        <div class="rightDiv">\n' +
                '                    <input   class="easyui-numberbox tchProdNum" label="教师机数目:"  style="width:100%"\n' +
                '                           data-options="editable:true,labelPosition:\'top\',min:0"/>\n' +
                '        </div>'+
                '    </div>';
            $('.modelTchTr:last').after(dom);
            $.parser.parse('.modelTchTr:last');
        })
    }

    $('#save').click(function () {
        // window.top.askDoSomething('确认','请确认信息填写完整',function () {
        if (currentItem == '#m-scanner') {
            window.top.askDelAction('确认','请确认信息填写完整',function () {
                var formData = $('#ff').serialize();
                var obj = {};
                obj.tchCode = currentList;
                formData = formData + '&' + $.param(obj);
                if (Object.keys(currentList).length != iTchNum) {
                    window.top.sendErrorMsg('应出库的教师机数目不符');
                    return false;
                }
                $.ajax({
                    url: baseUrl + 'mobile/backlog/confirmApply',
                    type: 'POST',
                    data: formData,
                    success: function (data) {
                        window.top.sendSuccessMsg(data);
                        window.top.upCountLog();
                    },
                    error: function (data) {

                        window.top.sendErrorMsg(data.responseText);
                    }
                });
            });
        } else if (currentItem == '#m-out') {
            window.top.askDelAction('确认','请确认信息填写完整',function () {
                var formData = $('#ff').serialize();
                var obj = {};
                obj.tchBackInfo = getTchBackInfo();
                formData = formData + '&' + $.param(obj);
                $.ajax({
                    url: baseUrl + 'mobile/backlog/confirmTchBack',
                    type: 'POST',
                    data: formData,
                    success: function (data) {
                        window.top.sendSuccessMsg(data);
                        window.top.upCountLog();

                    },
                    error: function (data) {

                        window.top.sendErrorMsg(data.responseText);
                    }
                });
            });


        }

    });


    $('#addShell').click(function () {
        var index = $('.modelTr').length ;
        var dom = '<div class="modelTr" style="margin-bottom: 10px;height: 60px;">\n' +
                '        <div class="leftDiv">\n' +
                '            <select  class="easyui-combobox" name="shellCode['+index+'][prodId]" label="选择平板种类:" data-options="editable:false,labelPosition:\'top\'" style="width:100%">\n' +
                '                <option value="" style="display: none" selected>请选择</option>\n' ;
                                        $.each(tabletShell,function (id,text) {
                                            dom += '<option value="'+id+'">'+text+'</option>\n' ;
                                        });
                dom+='            </select>\n' +
                '        </div>\n' +
                '        <div class="rightDiv">\n' +
                '            <input  class="easyui-numberbox" label="平板壳数目:" name="shellCode['+index+'][iNum]" style="width:100%"\n' +
                '                   data-options="editable:true,labelPosition:\'top\'"/>\n' +
                '        </div>\n' +
                '    </div>';
        $('.modelTr:last').after(dom);
        $.parser.parse('.modelTr:last');
    });

});

function delTch(target) {
    window.top.swal({title:'警告',text: '请确认要删除这条记录嘛？',type:'info',showCancelButton:true,confirmButtonText:'确定',cancelButtonText:'取消'}).then(function () {
        var dom = $(target).parent().parent();
        var tchId = dom.attr('nodeId');
        delete currentList[tchId];
        dom.remove();
        $('#tchId').combobox({
            loadFilter:function (data) {
                return filterData(data);
            }
        })
    },function () {
        return false
    });
}

/**
 * 获取教师直接入库信息
 *
 */
function getTchBackInfo() {
    var arr = [];
    $('.modelTchTr').each(function () {
        var item = {};
        item.prodId = $(this).find('.tchProd').combobox('getValue');
        item.prodNum = $(this).find('.tchProdNum').numberbox('getValue');
        if (item.prodId && item.prodNum && item.prodNum > 0 ){
            arr.push(item);
        }
    });
    return arr;
}

function filterData(data) {
    $.each(data,function (key,row) {
          if(row !== undefined){
              var id = row['value'];
              data[key]['disabled'] = false;
              if (currentList.hasOwnProperty(id)) {
                  data[key]['disabled'] = true;
              }
          }

    });
    return  data;
}


/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle = {
    setResult: function (code) {
        setTimeout(function () {
            var iProdId = $('#prodId').combobox('getValue'),
                prodName = $('#prodId').combobox('getText'),
                tchName = $('#tchId').combobox('getText'),
                tchId = $('#tchId').combobox('getValue');

            if(!iProdId || !tchId){
                window.top.sendErrorMsg('请选择平板类型或请选择教师');
                return false;
            }
            var newObj  = {};
                newObj.code  = code;
                newObj.iProdId = iProdId;
            currentList[tchId] = newObj;
            var str ='                      <tr nodeId="'+tchId+'">\n' +
                    '                            <td>'+tchName+'</td>\n' +
                    '                            <td>'+prodName+'</td>\n' +
                    '                            <td>'+code+'</td>\n' +
                    '                            <td><a onclick="delTch(this)">删除</a></td>\n' +
                    '                        </tr>';

            if($('#tchData tbody').find('tr').length <= 0){
                $('#tchData tbody').append(str);
            }else{
                $('#tchData tbody').find('tr').eq(0).before(str);
            }
            $('#tchId').combobox({
                loadFilter:function (data) {
                    return filterData(data);
                }
            });
            $('#tchId').combobox('setValue' , '');
            $('#code').textbox('setValue',code);
        }, 0);

    }
};